﻿
using DrCarDrive.Util;
using DrCarDrive.Wpf;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using MahApps.Metro.Controls;
using DrCarDrive.Wpf.WpfPage;
using System.Configuration;

namespace DrCarDrive.Wpf.WpfWindow
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class PasswordChangeWindow : MetroWindow
    {
        private static PasswordChangeWindow instance;

        private PasswordChangeWindow()
        {
            InitializeComponent();
            instance = this;
        }

        public static PasswordChangeWindow Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new PasswordChangeWindow();
                }

                return instance; 
            }
        }

        private void btnPasswordCancel_Click(object sender, RoutedEventArgs e)
        {
            txtCurrentPassword.Password = "";
            txtNewPassword.Password = "";
            txtConfirmPassword.Password = "";
        }

        private void btnPasswordSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var settings = config.AppSettings.Settings;
                if (txtNewPassword.Password == txtConfirmPassword.Password && txtNewPassword.Password!="")
                {
                    if (settings["admin.password"].Value == txtCurrentPassword.Password)
                    {
                        settings["admin.password"].Value = txtNewPassword.Password;
                        config.Save();
                        ConfigurationManager.RefreshSection("appSettings");

                        PasswordChangeWindow.Instance.Close();
                        MSGWindow msg = new MSGWindow("Successful", "Password changed successfully");
                        msg.ShowDialog();
                    }
                    else
                    {
                        MSGWindow msg = new MSGWindow("Error", "Current password is invalid");
                        msg.ShowDialog();
                    }                    
                }
                else
                {
                    MSGWindow msg = new MSGWindow("Error", "Please provide valid password and confirmation");
                    msg.ShowDialog();
                }
            }
            catch(Exception ex)
            {
                MSGWindow msg = new MSGWindow("Error", "Error occured when saving new password. Please try again");
                msg.ShowDialog();
            }
        }

        
    }
}
